package com.tianji.promotion.mq;

import com.tianji.common.constants.MqConstants;
import com.tianji.promotion.domain.dto.CouponMsgDto;
import com.tianji.promotion.service.IUserCouponService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class PromotionMqHandler {
    private final IUserCouponService userCouponService;
    @RabbitListener(bindings= @QueueBinding(
            value = @Queue(name = "coupon.receive.queue",durable = "true"),
            exchange = @Exchange(name = MqConstants.Exchange.PROMOTION_EXCHANGE,type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.COUPON_RECEIVE_KEY
    ))
    public void receiveCoupon(CouponMsgDto msg){
        if (msg == null){
            return;
        }
        log.info("收到优惠券领取消息：{}", msg);
        userCouponService.saveAndInrUserCoupon(msg);
    }
}
